<?php
namespace app\manage\controller;
use app\manage\controller\BaseController;
use think\facade\Db;
use think\facade\View;
use app\manage\model\Slide as Slidemodel;

class Slide extends BaseController
{
    public function index()
    {
    	$slide=new Slidemodel();
    	$key=input('key','');
        View::assign('key',$key);
        $slide=$slide->whereor('title','like','%'.$key.'%')
                        ->order('sort asc,id desc')
                        ->paginate(8,false,['query'=>request()->param()]);
        View::assign('slide',$slide);
        View::assign('count1', $slide->total());
       	return View::fetch();
    }
	public function ajax()
    {
    	$data=input('post.');
		$slide=new Slidemodel();
		if($data['type']=='slide_del'){
			$id=$data['id'];
			$info=$slide->destroy($id);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='slide_sort'){
			$arrlength=count($data['id']);
			$ar=[];
			for($x=0;$x<$arrlength;$x++)
			{
			    $ar[$x]=['id'=>$data['id'][$x], 'sort'=>$data['sort'][$x]];
			}
			$info=$slide->saveAll($ar);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
        return json(['code' => 0, 'message' => '失败']);
    }
	public function add()
    {
    	if(request()->isPost()){
			$data=input('post.');
			$validate = new \app\manage\validate\Slide;
			if(!$validate->check($data)){
				$this->error($validate->getError());
			}	
			if(!isset($data['isopen'])){
				$data['isopen']=0;
			}else{
				$data['isopen']=1;
			}
			$file = request()->file('');
			if(isset($file['img'])){
                try {
                    validate(['img'=>'fileSize:5242880|fileExt:jpg,png,gif,webp,jpeg'])->check($file);
                    $info=\think\facade\Filesystem::disk('public')->putFile("slide", $file['img']);
                    $data['img']='/storage/'.strtr($info, " \ ", " / ");
                } catch (\think\exception\ValidateException $e) {
                    $this->error($e->getMessage());
                }
			}
			$link=new Slidemodel();
			$link->data($data);
			$res=$link->save();
			if($res){
				$this->success('添加成功',url('slide/index',['st'=>1]));
			}else{
				$this->error('添加失败了');
			}
    	}
       return View::fetch();
    }
	public function edit()
    {
    	$slide = new Slidemodel();
    	if(request()->isPost()){
    		$data=input('post.');
			$validate = new \app\manage\validate\Slide;
			if(!$validate->scene('edit')->check($data)){
				$this->error($validate->getError());
			}	
			if(!isset($data['isopen'])){
				$data['isopen']=0;
			}else{
				$data['isopen']=1;
			}
			$file = request()->file('');
			if(isset($file['img'])){
                try {
                    validate(['img'=>'fileSize:5242880|fileExt:jpg,png,gif,webp,jpeg'])->check($file);
                    $info=\think\facade\Filesystem::disk('public')->putFile("slide", $file['img']);
                    $data['img']='/storage/'.strtr($info, " \ ", " / ");
                } catch (\think\exception\ValidateException $e) {
                    $this->error($e->getMessage());
                }
			}
			$res=$slide->update($data,['id' => input('id')]);
			if($res){
				$this->success('修改成功',url('slide/index',['st'=>1]));
			}else{
				$this->error('修改失败了');
			}
    	}
		$id=input('id',0);
		$data= $slide->find($id);
        View::assign('data',$data);
       return View::fetch();
    }
}
